package com.qiangesoft.miniapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiangesoft.miniapp.entity.SysUser;
import com.qiangesoft.miniapp.enums.UserStatusEnum;
import com.qiangesoft.miniapp.mapper.SysUserMapper;
import com.qiangesoft.miniapp.service.ISysUserService;
import com.qiangesoft.miniapp.util.RsaUtil;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户 服务实现类
 * </p>
 *
 * @author qiangesoft
 * @since 2024-07-16
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Override
    public SysUser selectByWxOpenId(String wxOpenId) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getWxOpenId, wxOpenId);
        return this.getOne(queryWrapper);
    }

    @Override
    public SysUser selectByUsername(String username) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUsername, username);
        return this.getOne(queryWrapper);
    }

    @Override
    public SysUser bind(String wxOpenId) {
        SysUser sysUser = new SysUser();
        sysUser.setUsername(wxOpenId);
        sysUser.setPassword(RsaUtil.rsaEncode("123456"));
        sysUser.setWxOpenId(wxOpenId);
        sysUser.setStatus(UserStatusEnum.ENABLE.getCode());
        sysUser.setDelFlag(false);
        baseMapper.insert(sysUser);
        return sysUser;
    }

    @Override
    public void unBind(Long userId) {
        SysUser sysUser = baseMapper.selectById(userId);
        sysUser.setWxOpenId(null);
        baseMapper.updateById(sysUser);
    }

}
